<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/vhuichen.ico">
  <link rel="mask-icon" href="/vhuichen.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"vhuichen.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"display","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="什么是 Modulemodule 表示代码编译的最小模块，也就是一个.m文件编译成.o目标文件，那么这个.o就是一个 module。而 modulemap 可以将多个 module 合并成一个 module。module 还有另一个功能就是充当 Framework 中 OC 跟 Swift 的桥接文件。module 可以用来取代C&#x2F;C++预言传统的头文件引入方式，C&#x2F;C++ 中">
<meta property="og:type" content="article">
<meta property="og:title" content="理解 iOS 中的 Modules">
<meta property="og:url" content="https://vhuichen.github.io/2021/05/iOS/Modules/Modules/index.html">
<meta property="og:site_name" content="vhuichen">
<meta property="og:description" content="什么是 Modulemodule 表示代码编译的最小模块，也就是一个.m文件编译成.o目标文件，那么这个.o就是一个 module。而 modulemap 可以将多个 module 合并成一个 module。module 还有另一个功能就是充当 Framework 中 OC 跟 Swift 的桥接文件。module 可以用来取代C&#x2F;C++预言传统的头文件引入方式，C&#x2F;C++ 中">
<meta property="og:locale">
<meta property="og:image" content="https://vhuichen.github.io/2021/05/iOS/Modules/Modules/20210512120910.png">
<meta property="og:image" content="https://vhuichen.github.io/2021/05/iOS/Modules/Modules/20210513181627.png">
<meta property="article:published_time" content="2021-05-08T16:00:00.000Z">
<meta property="article:modified_time" content="2022-09-04T03:07:17.704Z">
<meta property="article:author" content="vhuichen">
<meta property="article:tag" content="Swift">
<meta property="article:tag" content="Module">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://vhuichen.github.io/2021/05/iOS/Modules/Modules/20210512120910.png">


<link rel="canonical" href="https://vhuichen.github.io/2021/05/iOS/Modules/Modules/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://vhuichen.github.io/2021/05/iOS/Modules/Modules/","path":"2021/05/iOS/Modules/Modules/","title":"理解 iOS 中的 Modules"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>理解 iOS 中的 Modules | vhuichen</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">vhuichen</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录、分享</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section">Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section">About</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF-Module"><span class="nav-number">1.</span> <span class="nav-text">什么是 Module</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#include"><span class="nav-number">1.0.1.</span> <span class="nav-text">#include</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#import"><span class="nav-number">1.0.2.</span> <span class="nav-text">#import</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#import-1"><span class="nav-number">1.0.3.</span> <span class="nav-text">@import</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Module-maps"><span class="nav-number">1.0.4.</span> <span class="nav-text">Module maps</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Swift-Module"><span class="nav-number">1.0.5.</span> <span class="nav-text">Swift Module</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Swift-OC-%E9%80%9A%E4%BF%A1%E6%96%B9%E5%BC%8F"><span class="nav-number">1.0.6.</span> <span class="nav-text">Swift OC 通信方式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%AB%A0"><span class="nav-number">1.1.</span> <span class="nav-text">参考文章</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="vhuichen"
      src="/images/vhuichen.jpg">
  <p class="site-author-name" itemprop="name">vhuichen</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://vhuichen.github.io/2021/05/iOS/Modules/Modules/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/vhuichen.jpg">
      <meta itemprop="name" content="vhuichen">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vhuichen">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="理解 iOS 中的 Modules | vhuichen">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          理解 iOS 中的 Modules
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-05-09 00:00:00" itemprop="dateCreated datePublished" datetime="2021-05-09T00:00:00+08:00">2021-05-09</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2022-09-04 11:07:17" itemprop="dateModified" datetime="2022-09-04T11:07:17+08:00">2022-09-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="什么是-Module"><a href="#什么是-Module" class="headerlink" title="什么是 Module"></a>什么是 Module</h1><p><code>module</code> 表示代码编译的最小模块，也就是一个<code>.m</code>文件编译成<code>.o</code>目标文件，那么这个<code>.o</code>就是一个 <code>module</code>。而 <code>modulemap</code> 可以将多个 <code>module</code> 合并成一个 <code>module</code>。<code>module</code> 还有另一个功能就是充当 Framework 中 OC 跟 Swift 的桥接文件。<br><code>module</code> 可以用来取代C&#x2F;C++预言传统的头文件引入方式，C&#x2F;C++ 中单个文件被引入了N次就需要编译N次，而使用 <code>module</code> 只需要1次。<br>而 <code>module</code>和头文件之间则是通过 <code>modulemap</code> 关联的</p>
<h3 id="include"><a href="#include" class="headerlink" title="#include"></a>#include</h3><p>include 使用不当会导致头文件重复导入。<br>预编译时会将 include 文件递归导入进.m文件，<br>假如有M个文件，每个文件都引入N个头文件，那么编译时间变为 M * N</p>
<h3 id="import"><a href="#import" class="headerlink" title="#import"></a>#import</h3><p>改进了 include ，可以防止重复添加头文件</p>
<h3 id="import-1"><a href="#import-1" class="headerlink" title="@import"></a>@import</h3><p>@import 导入的就是一个 <code>module</code> 的头文件。在使用 <code>module</code> 之前我们需要先打开这个功能</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Defines Module = YES</span><br><span class="line">Module Map File = &quot;手动创建的文件路径&quot; //可选</span><br></pre></td></tr></table></figure>

<p><strong>module到底有什么作用呢？</strong><br>module 会先把头文件编译成二进制文件，哪里需要使用这个头文件都会直接使用这个编译好的二进制头文件，除非这个头文件自身发生改动。<br>同一个头文件只需要编译一次就行了</p>
<span id="more"></span>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">开启了 Defines Module 后，#include 跟 #import 都会被系统自动替换成 @import。</span><br></pre></td></tr></table></figure>

<h3 id="Module-maps"><a href="#Module-maps" class="headerlink" title="Module maps"></a>Module maps</h3><p>如果是动态库 Xcode 会自动生成一个<code>modulemap</code>, 静态库则需要手动生成。也可以自定义一个</p>
<img src="/2021/05/iOS/Modules/Modules/20210512120910.png" class title="img">

<p><strong>umbrella header “ModuleFrameworkA.h”</strong><br><code>umbrella</code> 可以翻译成 <code>集合体</code>，理解成头文件的集合，也就是将这个头文件下的所有头文件导入。<br>声明指定指定目录中的所有头文件都应包含在模块中</p>
<p><strong>export *</strong><br><code>*</code>号匹配所有的文件。表示导出所有的头文件，也就是 <code>umbrella header</code>中的头文件  </p>
<p><strong>module * { export * }</strong><br>将每一个导入的头文件，再次导出为子module，导出后就可以引入某个头文件  </p>
<p><strong>explicit module XXXX</strong><br>理解为显式导入一个子模块，并可以自定义子模块的名称</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">这里测试发现无法用#import&lt;A/a.h&gt; 方式导入显式子模块，但是 @import A.a 是可以的。</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">也可以自定义 modulemap 文件，并将OC的头文件引入，然后将路径引入到 import paths 中，同样也可以在 Swift 中引用</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">//还可以声明私有文件头文件，但只能做到提示开发者此文件为私有文件，并不能完全隔绝</span><br><span class="line">framework module ModuleAFramework_Private &#123;</span><br><span class="line">	header &quot;Student.h&quot;</span><br><span class="line">	export *</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Swift-Module"><a href="#Swift-Module" class="headerlink" title="Swift Module"></a>Swift Module</h3><p>而从 Swift 5.1 版本开始， <code>Module Stability</code> 允许 5.1 版本打的 Module 在 5.1 之后的任意版本运行。<br>开启 <code>Module Stability</code> 后，Framework 引入了一个全新的文件夹 <code>.swiftmodule</code>，包含 <code>.swiftinterface</code> 和 <code>.swiftmodule</code> 文件，作为公共接口的定义文件。<br>基于 swiftmodule 这个文件，编译器能够构造出可以给OC使用的头文件 </p>
<p>当开启 <code>Build Libraries for Distribution</code> 编译后生成的文件就够如下</p>
<img src="/2021/05/iOS/Modules/Modules/20210513181627.png" class title="img">
<p>可以看到生成了 <code>module.modulemap</code> 文件 跟 <code>swiftmodule</code> 文件夹。<br><code>module.modulemap</code> 文件是 OC 生成给 Swift 的接口描述<br><code>swiftmodule</code> 文件夹则是 Swift 生成给 OC 的接口描述，<code>swiftinterface</code> 文件作为 <code>swiftmodule</code> 文件的一个补充，不受任何编译器版本限制。  </p>
<h3 id="Swift-OC-通信方式"><a href="#Swift-OC-通信方式" class="headerlink" title="Swift OC 通信方式"></a>Swift OC 通信方式</h3><p><strong>在同一个项目中</strong><br>OC  调用  Swift  ，需要显式导入 XXX-Swift.h 文件。<br>Swift 调用 OC ，通过桥接文件，不需要显式导入。</p>
<p><strong>同一个 Framework 中</strong><br>OC  调用  Swift  ，需要显式导入 XXX-Swift.h 文件。<br>Swift 调用 OC ，通过modulemap，不需要显式导入。</p>
<p><strong>不在同一个 Framework 中</strong><br>OC  调用  Swift  ，需要显式导入 XXX-Swift.h 文件。<br>Swift 调用 OC ，通过modulemap，不需要显式导入。</p>
<h2 id="参考文章"><a href="#参考文章" class="headerlink" title="参考文章"></a>参考文章</h2><p><a target="_blank" rel="noopener" href="https://clang.llvm.org/docs/Modules.html#introduction">文档</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Swift/" rel="tag"># Swift</a>
              <a href="/tags/Module/" rel="tag"># Module</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/04/iOS/Allocations/Allocations/" rel="prev" title="Instruments 之 Allocations">
                  <i class="fa fa-chevron-left"></i> Instruments 之 Allocations
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/NetworkProtocol/SRWebSocket/SRWebSocket/" rel="next" title="SRWebSocket 源码解析">
                  SRWebSocket 源码解析 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vhuichen</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
